package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysReservation;
import com.mars.module.admin.mapper.SysVisitRecordsMapper;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysVisitRecordsRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysVisitRecords;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysVisitRecordsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 就诊记录业务层处理
 *
 * @author mars
 * @date 2024-04-12
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysVisitRecordsServiceImpl implements ISysVisitRecordsService {

    private final SysVisitRecordsMapper baseMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysVisitRecords add(SysVisitRecordsRequest request) {
        SysVisitRecords entity = SysVisitRecords.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysVisitRecordsRequest request) {
        SysVisitRecords entity = SysVisitRecords.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysVisitRecords getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysVisitRecords> pageList(SysVisitRecordsRequest request) {
        Page<SysVisitRecords> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysVisitRecords> query = this.buildWrapper(request);
        IPage<SysVisitRecords> pageRecord = baseMapper.selectPage(page, query);
        List<SysVisitRecords> records = pageRecord.getRecords();
        List<SysVisitRecords> recordsNew = new ArrayList<>();
        for (SysVisitRecords reservation : records) {
            reservation.setUserName(userMapper.selectById(reservation.getUserId()).getUserName());
            recordsNew.add(reservation);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysVisitRecords> list(SysVisitRecordsRequest request) {
        LambdaQueryWrapper<SysVisitRecords> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysVisitRecords> buildWrapper(SysVisitRecordsRequest param) {
        LambdaQueryWrapper<SysVisitRecords> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getSymptomDescribe())) {
            query.like(SysVisitRecords::getSymptomDescribe, param.getSymptomDescribe());
        }
        if (StringUtils.isNotBlank(param.getResult())) {
            query.like(SysVisitRecords::getResult, param.getResult());
        }
        if (StringUtils.isNotBlank(param.getPlan())) {
            query.like(SysVisitRecords::getPlan, param.getPlan());
        }
        return query;
    }

}
